﻿using System;
using System.Collections.Generic;
using System.Text;

using Obsidian.Edm;
using Obsidian.Action;
using Oak.Base;
using Oak.Model;
using Oak.Core.Logic;

namespace Oak.Core.Serv
{
    public class ActionServ : OakServ<OakSession>
    {

        public const string ART_CAT_FIELDS = "catId,parentId,catName,sortOrder,depth,path,modified,created,enabled";
        private const string ACTION_FIELDS = "actId,actName,actCatId,desc,modified,created";
        private const string LIST_ATTR_FIELDS = "itemsCount,pageNo,pageSize,pagesCount";

        public ActionServ(OakSession session) : base(session) { }

        public void List(ActionRequest req, ActionResponse res)
        {

            CoActionQry qry = req.GetModelByNameOrFirst<CoActionQry>("qry");
            string msg;
            ActionLG actionLG = new ActionLG(Session);
            ListAttrInfo listAttr;
            List<CoActionInfo> list = actionLG.GetActionList(qry, out listAttr, out msg);
            if (list == null)
            {
                res.Error(msg);
                return;
            }
            res.AddResult("actions", ACTION_FIELDS, list)
                .SetRelation("actCat", "actCatId", "actCats", "catId");
            res.AddResult("actCats", ART_CAT_FIELDS, "catId", "actCat", list);
            res.AddResult("listAttr", LIST_ATTR_FIELDS, listAttr);

        }

        public void Get(ActionRequest req, ActionResponse res)
        {
            ActReqParam param = req.GetParamByNameOrFirst("action");
            long actionId;
            if (!param.TryGetFirstLong("actId", out actionId))
            {
                res.Error("参数actionId错误");
                return;
            }
            ActionLG actionLG = new ActionLG(Session);
            string msg;
            CoActionInfo action = actionLG.GetActionById(actionId, out msg);
            if (action == null)
            {
                res.Error(msg);
                return;
            }
            ActionResult rsActions = res.AddResult("actions", ACTION_FIELDS);
            rsActions.AddModel(action);
        }

        public void Add(ActionRequest req, ActionResponse res)
        {
            CoActionInfo action = req.GetModelByNameOrFirst<CoActionInfo>("action");
            ActionLG actionLG = new ActionLG(Session);
            string msg;
            action = actionLG.AddAction(action, out msg);
            if (action == null)
            {
                res.Error(msg);
                return;
            }
            ActionResult rsActions = res.AddResult("actions", ACTION_FIELDS);
            rsActions.AddModel(action);
        }


        public void Update(ActionRequest req, ActionResponse res)
        {
            CoActionInfo action = req.GetModelByNameOrFirst<CoActionInfo>("action");
            ActionLG actionLG = new ActionLG(Session);
            string msg;
            action = actionLG.UpdateAction(action, out msg);
            if (action == null)
            {
                res.Error(msg);
                return;
            }
            ActionResult rsActions = res.AddResult("actions", ACTION_FIELDS);
            rsActions.AddModel(action);
        }


        public void Delete(ActionRequest req, ActionResponse res)
        {
            ActReqParam param = req.GetParamByNameOrFirst("action");
            long actionId;
            if (!param.TryGetFirstLong("actId", out actionId))
            {
                res.Error("参数actionId错误");
                return;
            }

            ActionLG actionLG = new ActionLG(Session);
            string msg;
            bool succ = actionLG.DeleteAction(actionId, out msg);
            if (!succ)
            {
                res.Error(msg);
                return;
            }

        }


    }
}
